package com.example.work3.service;

import com.example.work3.entity.Course;
import com.example.work3.entity.CourseSelection;
import com.example.work3.pojo.QueryAccount;
import com.example.work3.repository.CourseRepository;
import com.example.work3.repository.CourseSelectionRepository;
import jakarta.transaction.Transactional;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class CourseSelectService {
    @Autowired
    private CourseSelectionRepository courseSelectionRepository;
    @Autowired
    private CourseRepository courseRepository;
    @Autowired
    private StudentService studentService;
    //查询已选课程
    public List<Course> selectCourse(String studentId){
        List<CourseSelection> byStudentId = courseSelectionRepository.findByStudentId(Integer.parseInt(studentId));
        return byStudentId.stream().map(CourseSelection::getCourse).toList();
    }
    //查询未选课程
    public List<Course> noSelectCourse(String studentId){
        List<Course> courses = selectCourse(studentId);
       return courseRepository.findAll().stream().filter(course -> !courses.contains(course)).toList();
    }
    //选课
    public boolean selectCourse(String studentId, Course course){
        CourseSelection courseSelection = new CourseSelection();
        courseSelection.setStudent(studentService.getStudentById(Integer.parseInt(studentId)));
        courseSelection.setCourse(course);
        return courseSelectionRepository.save(courseSelection).getId()!=0;
    }
    //退课
    @Transactional
    public boolean dropCourse(String studentId, Course course){
        return courseSelectionRepository.deleteByStudentIdAndCourseId(Integer.parseInt(studentId),course.getId())>0;
    }
    //增加和修改课程
    public Course addORUpdateCourse(Course course){
        return courseRepository.save(course);
    }
    //删除课程
    public String deleteCourse(Course course){
        if (courseSelectionRepository.existsByCourseId(course.getId())) {
            return "该课程已被选，无法删除";
        }else {
            try {
                courseRepository.delete(course);
                return String.valueOf(true);
            }catch (Exception e){
                return String.valueOf(false);
            }
        }
    }
    //动态条件查询课程
    public List<Course> findCourses(Course course) {
//        System.out.println(course);
       if (course.getName()!=null && course.getCredits()!=0) {
            return courseRepository.findByNameAndCredits(course.getName(),course.getCredits());
        } else if (course.getName()!=null) {
            return courseRepository.findByName(course.getName());
        } else if (course.getCredits()!=0) {
            return courseRepository.findByCredits(course.getCredits());
        }else return courseRepository.findAll();
    }
    //动态条件分页查询选课信息
    public Page<CourseSelection> findCoursesByPage(QueryAccount queryAccount) {
        if (queryAccount == null || queryAccount.getPage() == null || queryAccount.getPageSize() == null) {
            return null;
        } else {
            //无条件查询
            if (queryAccount.getCourseName()==null && queryAccount.getStudentId()==null) {
                return courseSelectionRepository.findAll(PageRequest.of(Integer.parseInt(queryAccount.getPage()),
                        Integer.parseInt(queryAccount.getPageSize())));
            } else {
                if (queryAccount.getCourseName() !=null && queryAccount.getStudentId() !=null) {
                    //全条件查询
                    return courseSelectionRepository.findAllByStudent_IdAndCourse_Name(
                            Integer.parseInt(queryAccount.getStudentId()), queryAccount.getCourseName(),
                            PageRequest.of(Integer.parseInt(queryAccount.getPage()),
                                    Integer.parseInt(queryAccount.getPageSize()))
                    );
                } else if (queryAccount.getCourseName() !=null) {
                    //课程查询学生
                    return courseSelectionRepository.findAllByCourse_Name(queryAccount.getCourseName(),
                            PageRequest.of(Integer.parseInt(queryAccount.getPage()),
                                    Integer.parseInt(queryAccount.getPageSize())));
                } else {
                    //学生查询课程
                    return courseSelectionRepository.findAllByStudent_Id(Integer.parseInt(queryAccount.getStudentId()),
                            PageRequest.of(Integer.parseInt(queryAccount.getPage()),
                                    Integer.parseInt(queryAccount.getPageSize())));
                }
            }
        }
    }
}
